﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
namespace LitTools
{
    /// <summary>
    /// 拖拽过滤器UI
    /// </summary>
    public class DragFilterMono : MonoWithFilterQueue, IBeginDragHandler, IDragHandler, IEndDragHandler
    {
        //缓存开始拖拽的物体
        private List<GameObject> beginDragGos = new();
        //射线穿透结果
        private List<RaycastResult> raycastResult = new();

        public void OnBeginDrag(PointerEventData eventData)
        {
            EventSystem.current.RaycastAll(eventData, raycastResult);
            raycastResult.RemoveAt(0);
            var targets = PassFilterQueue(raycastResult);
            for (int i = 0; i < targets.Count; i++)
            {
                beginDragGos.Add(targets[i].gameObject);
                ExecuteEvents.Execute(targets[i].gameObject, eventData, ExecuteEvents.beginDragHandler);
            }
        }

        public void OnDrag(PointerEventData eventData)
        {
            for (int i = 0; i < beginDragGos.Count; i++)
            {
                ExecuteEvents.Execute(beginDragGos[i], eventData, ExecuteEvents.dragHandler);
            }
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            for (int i = 0; i < beginDragGos.Count; i++)
            {
                ExecuteEvents.Execute(beginDragGos[i], eventData, ExecuteEvents.endDragHandler);
            }
            beginDragGos.Clear();
        }
    }
}